// 计算两个经纬度之间的距离
function calcDistance(pos1, pos2) {
	let {
		latitude: la1,
		longitude: lo1
	} = pos1
	let {
		latitude: la2,
		longitude: lo2
	} = pos2
	let EARTH_RADIUS = 6378.137; //地球半径,单位千米
	let rad = function(d) {
		return d * Math.PI / 180.0
	}
	//拿到x1,x2,y1,y2
	// 1.求出la1~2的差
	let radLat1 = parseFloat(rad(la1))
	let radLat2 = parseFloat(rad(la2))
	let a = radLat1 - radLat2
	let b = rad(lo1) - rad(lo2)
	let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
		Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
	s = s * EARTH_RADIUS;
	s = Math.round(s * 10000) / 10000;
	return s;
}

// 得到两地距离后,距离格式化计算
function distanceToStr(item) {
	if (item) {
		item = item.toFixed(2)
		if (item > 99) {
			return '距离太远'
		} else if (item >= 1) {
			return item + ' 公里'
		} else {
			return item * 1000 + " 米"
		}
	}
}
function test(){
	console.log("test test")
}
export {
	calcDistance,
	distanceToStr,
	test
}
